using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace JumploaderWrapper
{
  public enum JumploaderLookAndFeel
  {
    System, CrossPlatform
  }

  [DefaultProperty("Name")]
  [ToolboxData("<{0}:JumploaderControl runat=\"server\"></{0}:JumploaderControl>")]
  public class JumploaderControl : AppletControl
  {
    /// <summary>
    /// current version of jumploader_z:2.14.2
    /// Note if loadImageEditor or scaledInstancePreserveMetadata it will automatically add the correct resources
    /// </summary>
    public string default_archive = "JumploaderResource.aspx?Resource=jumploader_z&Type=applet";
    public string core_archive = "JumploaderResource.aspx?Resource=jl_core_z&Type=applet";
    /// <summary>
      /// I havent tested the ftp extension, this is mostly for informational purposes
      /// </summary>
    public string full_archive
    {
        get
        {
            return mediautl_archive + "," + sanselan_archive + "," + default_archive;
        }
    }
    public string mediautl_archive = "JumploaderResource.aspx?Resource=mediautil_z&Type=applet";
    public string sanselan_archive = "JumploaderResource.aspx?Resource=sanselan_z&Type=applet";
    //private const string DEFAULT_ARCHIVE = "~/jumploader_z.jar";
    private const string DEFAULT_CODE = "jmaster.jumploader.app.JumpLoaderApplet.class";

    private const string UPLOADER_CONFIG_CATEGORY = "UploaderConfig";
    private const string VIEW_CONFIG_CATEGORY = "ViewConfig";
    private const string APPLET_CONFIG_CATEGORY = "AppletConfig";

    // uc_
    private const string UPLOAD_URL_NAME = "uc_uploadURL";
    private const string MAX_FILE_LENGTH_NAME = "uc_maxFileLength";
    private const string DIRECTORY_ENABLED = "uc_directoriesEnabled";
    private const string DUPLICATE_FILE_ENABLED = "uc_duplicateFileEnabled";
    private const string MAX_FILE_LIST = "uc_maxFiles";
    private const string UPLOAD_THREAD_COUNT = "uc_uploadThreadCount";
    private const string UC_IMAGE_EDITOR_ENABLED = "uc_imageEditorEnabled";
    private const string UC_PRESERVE = "uc_scaledInstancePreserveMetadata";
    // vc_
    private const string LOOK_AND_FEEL = "vc_lookAndFeel";
    private const string VIEW_FILE_LIST_VISIBLE = "vc_mainViewFileListViewVisible";
    private const string VIEW_FILE_TREE_VISIBLE = "vc_mainViewFileTreeViewVisible";
    private const string VIEW_LOGO_ENABLED = "vc_mainViewLogoEnabled";
    private const string DISABLED_FILESYSTEM = "vc_disableLocalFileSystem";


    public JumploaderControl()
    {
      Archive = core_archive;
      Code = DEFAULT_CODE;

      Width = 600;
      Height = 400;
      MayScript = true;
      Align = AppletAlign.Middle;

      //by default don't show file types that may be secure
      string securePattern = "";
      foreach (string s in MultUploadHandler.disabledFileTypes)
          securePattern += s.Replace(".", "") + "|";
      securePattern = securePattern.Substring(0, securePattern.Length - 1);

      securePattern = "(?i)^(.*\\.(?!"+securePattern+")[^\\.]*)|([^\\.]+)$";
      fileNamePattern = securePattern;
    }

    /// <summary>
    /// Regular Expression for the file name Pattern. By default this is set to dissallow file types such as aspx, asp, js, etc.
    /// </summary>
    public string fileNamePattern
    {
        get
        {
            if (!AppletParams.ContainsKey("uc_fileNamePattern"))
                return null;

            return AppletParams["uc_fileNamePattern"];
        }
        set
        {
            AppletParams["uc_fileNamePattern"] = value.ToString();
            AppletParams["vc_fileNamePattern"] = value.ToString();
        }
    }

    public bool ViewLogo
    {
      get
      {
        if (!AppletParams.ContainsKey(VIEW_LOGO_ENABLED))
          return true;
        else
          return Convert.ToBoolean(AppletParams[VIEW_LOGO_ENABLED]);
      }
      set
      {
        AppletParams[VIEW_LOGO_ENABLED] = value.ToString();
      }
    }

    /// <summary>
    /// View the left hand file tree, false by default
    /// </summary>
    public bool FileTreeVisible
    {
      get
      {
        if (!AppletParams.ContainsKey(VIEW_FILE_TREE_VISIBLE))
          return true;
        else
          return Convert.ToBoolean(AppletParams[VIEW_FILE_TREE_VISIBLE]);
      }
      set
      {
          AppletParams[VIEW_FILE_TREE_VISIBLE] = value.ToString();
          if (value)
              DisabledFileSystem = false;
      }
    }

    /// <summary>
    /// View the top file list, false by default
    /// </summary>
    public bool FileListVisible
    {
      get
      {
        if (!AppletParams.ContainsKey(VIEW_FILE_LIST_VISIBLE))
          return true;
        else
          return Convert.ToBoolean(AppletParams[VIEW_FILE_LIST_VISIBLE]);
      }
      set
      {
        AppletParams[VIEW_FILE_LIST_VISIBLE] = value.ToString();
        if (value)
            DisabledFileSystem = false;
      }
    }


    public JumploaderLookAndFeel LookAndFeel
    {
      get 
      {
        if (!AppletParams.ContainsKey(LOOK_AND_FEEL))
        {
          return JumploaderLookAndFeel.CrossPlatform;
        }
        else
        {
          switch (AppletParams[LOOK_AND_FEEL])
          {
            case "system":
              return JumploaderLookAndFeel.System;
            case "crossPlatform":
              return JumploaderLookAndFeel.CrossPlatform;
            default:
              throw new ArgumentException("Invalid Value found in look and Feel");
          }
        }
      }
      set
      {
        switch (value)
        {
          case JumploaderLookAndFeel.System:
            AppletParams[LOOK_AND_FEEL] = "system";
            break;
          case JumploaderLookAndFeel.CrossPlatform:
            AppletParams[LOOK_AND_FEEL] = "crossSystem";
            break;
          default:
            throw new ArgumentException("If you got this you really screwed up, invalid value for look and feel.");
        }
      }
    }
         
    public int UploadThreadCount
    {
      get
      {
        if (!AppletParams.ContainsKey(UPLOAD_THREAD_COUNT))
          return 1;
        else
          return Convert.ToInt32(AppletParams[UPLOAD_THREAD_COUNT]);
      }
      set
      {
        AppletParams[UPLOAD_THREAD_COUNT] = value.ToString();
      }
    }

    public int MaxFiles
    {
      get
      {
        if (!AppletParams.ContainsKey(MAX_FILE_LIST))
          return -1;
        else
          return Convert.ToInt32(AppletParams[MAX_FILE_LIST]);
      }
      set
      {
        AppletParams[MAX_FILE_LIST] = value.ToString();
      }
    }

    public bool DuplicateFileEnabled
    {
      get
      {
        if (!AppletParams.ContainsKey(DUPLICATE_FILE_ENABLED))
          return false;
        else
          return Convert.ToBoolean(AppletParams[DUPLICATE_FILE_ENABLED]);
      }
      set
      {
        AppletParams[DUPLICATE_FILE_ENABLED] = value.ToString();
      }
    }

    /// <summary>
    /// Required: The path of the page that will handle the uploaded files. Ex. UploadURL="/FileSave/Process.aspx"
    /// </summary>
    public string UploadURL
    {
      get { return AppletParams[UPLOAD_URL_NAME]; }
      set { AppletParams[UPLOAD_URL_NAME] = value; }
    }

    public int MaxFileLength
    {
      get
      {
        if (!AppletParams.ContainsKey(MAX_FILE_LENGTH_NAME))
          return -1;
        else
          return Convert.ToInt32(AppletParams[MAX_FILE_LENGTH_NAME]);
      }
      set
      {
        AppletParams[MAX_FILE_LENGTH_NAME] = value.ToString();
      }
    }

    /// <summary>
    /// shows whether folder addition enabled (will expand and add all files)
    /// </summary>
    public bool DirectoryEnabled
    {
      get
      {
        if (!AppletParams.ContainsKey(DIRECTORY_ENABLED))
          return false;
        else
          return Convert.ToBoolean(AppletParams[DIRECTORY_ENABLED]);
      }
      set
      {
        AppletParams[DIRECTORY_ENABLED] = value.ToString();
      }
    }

    public bool DisabledFileSystem
    {
        get
        {
            if (!AppletParams.ContainsKey(DISABLED_FILESYSTEM))
                return true;
            else
                return Convert.ToBoolean(AppletParams[DISABLED_FILESYSTEM]);
        }
        set
        {
            AppletParams[DISABLED_FILESYSTEM] = value.ToString();
        }
    }

    /// <summary>
    /// Sets uc_imageEditorEnabled", "true" also adds the mediautil_z to be loaded in the applet
    /// Note: If you are manually setting the archive you must do it after this function
    /// </summary>
    public bool loadImageEditor
    {
        get
        {
            if (!AppletParams.ContainsKey(UC_IMAGE_EDITOR_ENABLED))
                return false;
            else
                return Convert.ToBoolean(AppletParams[UC_IMAGE_EDITOR_ENABLED]);
        }
        set
        {
            AppletParams[UC_IMAGE_EDITOR_ENABLED] = value.ToString();
            resetArchive();
        }
    }

    /// <summary>
    /// Sets uc_scaledInstancePreserveMetadata", "true" also adds the sanselan_z to be loaded in the applet
    /// Note: If you are manually setting the archive you must do it after this function
    /// </summary>
    public bool scaledInstancePreserveMetadata
    {
        get
        {
            if (!AppletParams.ContainsKey(UC_PRESERVE))
                return false;
            else
                return Convert.ToBoolean(AppletParams[UC_PRESERVE]);
        }
        set
        {
            AppletParams[UC_PRESERVE] = value.ToString();
            resetArchive();
        }
    }

      private void resetArchive()
      {
          Archive = core_archive;
          if (loadImageEditor || !DisabledFileSystem)
              Archive = default_archive;
          if (scaledInstancePreserveMetadata)
              Archive = sanselan_archive + "," + Archive;
          //if (lossLessJPEGTransitions) //todo if needed
          //    Archive = mediautl_archive + "," + Archive;
      }

  }
}
